<template>
  <div class="set">
    <div class="set_ct">
      <Form ref="formValidate"
            :model="formData"
            :rules="ruleValidate"
            :label-width="100"
            class="set_ct_form">
        <FormItem label="设置临界值:"
                  prop="critical">
          <!-- <Select v-model="formData.critical">
                <Option value="1">1</Option>
                <Option value="2">2</Option>
                <Option value="3">3</Option>
                <Option value="4">4</Option>
                <Option value="5">5</Option>
            </Select> -->
          <Input v-model="formData.critical"
                 :maxlength="2"
                 placeholder="可选择范围为1到10之间"
                 @on-keyup="changecritical"
                 @on-blur="priceBlur"
                 style="width:280px;"></Input>
          <!-- <Input v-model="formData.critical" style="width:180px;" ></Input> -->
          <span style="padding-left:8px;">秒</span>
        </FormItem>
        <FormItem>
          <Button type="primary"
                  class="btn"
                  @click="handleSubmit">确认</Button>
          <Button class="btn"
                  style="margin-left:20px;"
                  @click="cancel">取消</Button>
        </FormItem>
      </Form>
    </div>
  </div>
</template>
<script>
import { getDicQuery, setDicSave } from '@/api/setting'
import { routeEqual } from '@/libs/util'
import './index.less'
export default {
  data () {
    return {
      title: '刷卡器与服务器临界值配置',
      formData: {
        critical: '5'
      },
      ruleValidate: {
        critical: [{ required: true, type: 'string', message: '临界值不能为空', trigger: 'blur' }, { required: true, message: '可选择范围为1到10之间', trigger: 'blur', pattern: /^([1-9]||10)$/ig }]

      }
    }
  },
  methods: {
    submitForm () {
      getDicQuery({
        'dictKeys': 'serverCriticalValue',
        'merchantId': this.$store.state.user.merchantId,
        'msType': '0'
      }).then((res) => {
        if (res.data.status === 200) {
          if (res.data.data[0].serverCriticalValue) {
            this.formData.critical = res.data.data[0].serverCriticalValue
          }
        } else {
          this.$Message.error(`${res.data.error}`)
        }
      })
    },
    cancel () {
      // this.formData.critical = null
      this.closeModal()
    },
    changecritical () {
      this.formData.critical = this.formData.critical.replace(/[^\d]/g, '').replace(/^0{1,}/g, '')
      if (this.formData.critical > 10) {
        this.formData.critical = this.formData.critical.substring(0, 1)
      }
    },
    priceBlur () {
      this.formData.critical = this.formData.critical.replace(/[^\d]/g, '').replace(/^0{1,}/g, '')
    },
    // 关闭弹窗
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'global_setting'
        })
      })
    },
    handleSubmit () {
      this.$refs.formValidate.validate((val) => {
        if (val) {
          setDicSave({
            'serverCriticalValue': this.formData.critical,
            'merchantId': this.$store.state.user.merchantId,
            'msType': '0'
          }).then(res => {
            if (res.data.status === 200) {
              this.$Message.success('修改成功')
              this.closeModal()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    }
  },
  mounted () {
    this.submitForm()
    if (!this.hasPermission('moms:globalsetting:serverthreshold')) {
      this.$router.replace({ name: 'error_401' })
    }
  }
}
</script>
